package tech.yunland.shop.domain.model;

import tech.yunland.activity.domain.imodel.BaseModel;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Set;

/**
 * 采购单
 *
 * @author 文卡<wkwenka@gmail.com>  on 17-2-13.
 */

@Entity
@Table(name = "purchase_bill")
public class PurchaseBill extends Bill implements BaseModel {

    @OneToMany(mappedBy = "owner")
    protected Set<PurchaseBillEntry> entries; // 分录


    @Column(name = "biz_type", nullable = false)
    private Integer biz_type = Integer.valueOf(0); // 0 代表欲采购 1表示已采购

    @Transient
    private String bizType;

    @Transient
    private BigDecimal amount;

    @Column(name = "processdef_id")
    private String processdefId;//流程id

    @Column(name = "processdef_name")
    private String processdefName;//流程名称

    //最近审批人
    @Column(name = "recently_approval")
    public String recentlyApproval;

    private String result;
    @Column(length = 50)
    private String handerName;

    @Transient
    private Integer entrySize; //实体数量

    @Transient
    private String entryName; //分录商品名称

    @Column(name = "kind_code")
    private Integer kindCode;//采购类型 10：现购 20：赊购

    public String getHanderName() {
        return handerName;
    }

    public void setHanderName(String handerName) {
        this.handerName = handerName;
    }

    public Integer getBiz_type() {
        return biz_type;
    }

    public void setBiz_type(Integer biz_type) {
        this.biz_type = biz_type;
    }

    public Set<PurchaseBillEntry> getEntries() {
        return entries;
    }

    public void setEntries(Set<PurchaseBillEntry> entries) {
        this.entries = entries;
    }

    public BigDecimal getAmount() {
        BigDecimal total = BigDecimal.ZERO;
        if (entries != null) {
            for (PurchaseBillEntry entry : entries) {
                if (entry.getState() != null && entry.getState().intValue() == 1) {
                    total = total.add(entry.getAmount());
                }
            }
        }
        return total;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getBizType() {
        switch (biz_type.intValue()) {
            case 1:
                bizType = "已采购";
                break;
            case 0:
                bizType = "待采购";
                break;
        }
        return bizType;
    }

    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    public String getProcessdefId() {
        return processdefId;
    }

    public void setProcessdefId(String processdefId) {
        this.processdefId = processdefId;
    }

    public String getProcessdefName() {
        return processdefName;
    }

    public void setProcessdefName(String processdefName) {
        this.processdefName = processdefName;
    }

    public String getRecentlyApproval() {
        return recentlyApproval;
    }

    @Override
    public void setRecentlyApproval(String recentlyApproval) {
        this.recentlyApproval = recentlyApproval;
    }

    public String getResult() {
        return result;
    }

    @Override
    public void setResult(String result) {
        this.result = result;
    }

    public Integer getEntrySize() {
        int i = 0;
        for (PurchaseBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() != -1) {
                i++;
            }
        }
        return Integer.valueOf(i);
    }

    public void setEntrySize(Integer entrySize) {
        this.entrySize = entrySize;
    }

    public String getEntryName() {
        String names = "";
        for (PurchaseBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() != -1) {
                names += entry.getGoodsName() + ",";
            }
        }
        return names.length() > 1 ? names.substring(0, names.length() - 1) : "";
    }

    public void setEntryName(String entryName) {
        this.entryName = entryName;
    }

    public Integer getKindCode() {
        return kindCode;
    }

    public void setKindCode(Integer kindCode) {
        this.kindCode = kindCode;
    }
}